package com.zjwd.web.controller.system;

import com.zjwd.common.annotation.Log;
import com.zjwd.common.core.controller.BaseController;
import com.zjwd.common.core.domain.AjaxResult;
import com.zjwd.common.core.domain.entity.SysPlatform;
import com.zjwd.common.core.domain.entity.SysPlatformUser;
import com.zjwd.common.core.domain.entity.SysUser;
import com.zjwd.common.core.domain.model.LoginUser;
import com.zjwd.common.core.page.TableDataInfo;
import com.zjwd.common.enums.BusinessType;
import com.zjwd.common.utils.StringUtils;
import com.zjwd.framework.web.service.TokenService;
import com.zjwd.system.domain.SysNotice;
import com.zjwd.system.service.ISysPlatformService;
import com.zjwd.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 系统平台controller
 *
 * @author liquan
 */
@RestController
@RequestMapping("/system/platform")
public class SysPlatformController extends BaseController {

    @Autowired
    private ISysPlatformService platformService;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private ISysUserService userService;

    /**
     * 获取系统平台列表
     */
    @PreAuthorize("@ss.hasPermi('system:platform:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysPlatform platform) {
        startPage();
        List<SysPlatform> list = platformService.selectPlatformList(platform);
        return getDataTable(list);
    }

    /**
     * 根据当前登录用户获取系统平台列表
     */
    @GetMapping("/selectPlatformListByUser")
    public TableDataInfo selectPlatformListByUser() {
        startPage();
        List<SysPlatformUser> list = platformService.selectPlatformListByUser(getUserAccount());
        return getDataTable(list);
    }

    /**
     * 当前登录用户切换系统平台
     *
     * @param platformCode 系统平台编号
     * @return 结果
     */
    @PostMapping("/switchPlatform")
    public AjaxResult switchPlatform(String platformCode) {
        SysPlatformUser platformUser = platformService.switchPlatform(platformCode, getUserAccount());
        // 更新缓存
        LoginUser loginUser = getLoginUser();
        loginUser.setPlatform(platformUser);
        // 更新缓存用户信息
        tokenService.setLoginUser(loginUser);
        return success();
    }

    /**
     * 新增
     */
    @PreAuthorize("@ss.hasPermi('system:platform:add')")
    @Log(title = "系统平台", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysPlatform platform) {
        if (!platformService.checkCodeUnique(platform)) {
            return error("新增系统平台'" + platform.getCode() + "'失败，系统平台编码已存在");
        }
        if (!platformService.checkNameUnique(platform)) {
            return error("新增系统平台'" + platform.getName() + "'失败，系统平台名称已存在");
        }
        platform.setCreateBy(getUserAccount());
        platformService.insert(platform);
        // 刷新当前登录用户的redis缓存
        LoginUser loginUser = getLoginUser();
        SysUser user = userService.selectUserByUserAccount(loginUser.getUserAccount());
        loginUser.setUser(user);
        tokenService.setLoginUser(loginUser);
        return success();
    }

}
